import { setToken, getToken, removeToken } from '@/utils/auth'
import { login, getName, gettouxiang } from '@/api/user'
import { resetRouter } from '@/router/index'
// import { resolve } from 'core-js/es6/promise'
// import { reject } from 'core-js/fn/promise'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    infor: {}
  },
  getters: {},
  mutations: {
    settoken(state, val) {
      state.token = val
      setToken(val)
    },
    setname(state, val) {
      state.infor = val
    },
    cleartoken(state) {
      state.token = ''
      state.infor = {}
      removeToken()
      resetRouter()
    }
  },
  actions: {
    // 登陆的方法
    login2(store, data) {
      return new Promise((resolve, reject) => {
        login(data).then(res => {
          // console.log(res)
          store.commit('settoken', res.data)
          resolve(res)
        }).catch(err => { console.log(err) })
      })
    },
    // 获取用户名和头像的方法
    async setinfor(store) {
      const res = await getName()
      const res2 = await gettouxiang(res.data.userId)
      // console.log(res2)
      // 合并数据
      res2.data.username = '为爱冲锋的勇士'
      res2.data.staffPhoto = 'https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2022%2F1113%2Fc05f5ea1j00rl9lfb0017c000hs00rrg.jpg&thumbnail=660x2147483647&quality=80&type=jpg'
      const data1 = { ...res.data, ...res2.data }
      store.commit('setname', data1)
      return data1
    },
    // 退出的方法
    logout(store) {
      store.commit('cleartoken')
    }
  }
}
